package cn.krui.project.controller.system;

import java.security.NoSuchAlgorithmException;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.SysLoginInfo;
import cn.krui.helper.tools.JsonUtils;
import cn.krui.helper.tools.RedisUtils;
import cn.krui.project.service.system.LoginService;
import cn.krui.project.bean.system.sys_user.sys_user;
import cn.krui.project.bean.system.sys_user.sys_user_login;
import cn.krui.project.bean.system.sys_user.sys_user_pwd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;  

/**
 * @program: project-system
 * @description: 后台登录
 * @author: zc
 * @create: 2019-10-16
 **/
@Api(tags = "管理-登录", description = "管理后台登录")
@RestController
@RequestMapping("/system/syslogin")
@CrossOrigin
public class LoginController extends SystemBaseController {

	private static Logger loggerVisit = LoggerFactory.getLogger("VISIT");

	@Autowired
	private LoginService loginService;
	@Autowired
	private RedisUtils rUtils;

	@PostMapping(value = "/login")
	@ApiOperation(value = "后台系统用户登录", httpMethod = "POST")
	public AppResult<SysLoginInfo> login(sys_user_login user) throws NoSuchAlgorithmException {
		AppResult<SysLoginInfo> app = new AppResult<SysLoginInfo>();
		if (StringUtils.isEmpty(user.getUser_name()) || StringUtils.isEmpty(user.getPasswd())) {
			app.no("登录名或密码错误");
			return app;
		}
		sys_user sysUser = loginService.sysLogin(user);
		if (sysUser == null) {
			app.no("登录名或密码错误");
			return app;
		}
		sysUser.setPasswd("");
		if (sysUser.getStatus() != 1) {
			app.no("您的账号已被禁用，请联系管理员");
			return app;
		}
		if (sysUser.getError_count() > 0) {
			app.no("密码错误");
			return app;
		}
//        if(sysUser.getError_count()>=3) {
//        	if(!StringUtil.isEmpty(user.getImg_uuid())) { 
//            	app.no("图形验证码错误");
//    	    	return app;
//        	}
//        	app.no("输入图形验证码");
//	    	return app;
//        }
		SysLoginInfo sysLoginInfo = new SysLoginInfo();
		sysLoginInfo.setCreate_time(new Date());
		sysLoginInfo.setUser_type("1");
		sysLoginInfo.setToken(sysUser.getToken());
		sysLoginInfo.setUid(sysUser.getId());
		sysLoginInfo.setRole_menu(sysUser.getRole_menu());
		sysLoginInfo.setRole_permissions(sysUser.getRole_permissions());
		//权限路径按模块分组减少过滤耗时
		sysLoginInfo.setRole_permissions_map(loginService.rolePermissionsToMap(sysUser.getRole_permissions()));
		sysLoginInfo.setUser_name(sysUser.getUser_name());
		sysLoginInfo.setGroup_id(sysUser.getGroup_id());
		app.setDataSingle(sysLoginInfo);
		rUtils.set("login_token:" + sysUser.getToken(), JsonUtils.toJson(sysLoginInfo));
		rUtils.delete("login_token:" + sysUser.getOld_token());
		loggerVisit.error(String.format("%s=%s", sysUser.getToken(), user.getUser_name()));
		app.ok("登录成功");
		return app;
	}

	@GetMapping(value = "/info")
	@ApiOperation(value = "后台系统登录信息", httpMethod = "GET")
	public AppResult<SysLoginInfo> info() {
		AppResult<SysLoginInfo> app = new AppResult<SysLoginInfo>();
		Object object = rUtils.get("login_token:" + super.getToken());
		SysLoginInfo sysLoginInfo = JsonUtils.toObject(object.toString(), SysLoginInfo.class);
		app.setDataSingle(sysLoginInfo);
		app.ok("获取成功");
		return app;
	}

	@SuppressWarnings("rawtypes")
	@PostMapping(value = "/logout")
	@ApiOperation(value = "退出登录", httpMethod = "POST")
	public AppResult logout() {
		AppResult app = new AppResult();
		rUtils.delete("login_token:" + super.getToken());
		app.ok();
		return app;
	}

	@SuppressWarnings("rawtypes")
	@PostMapping(value = "/repwd")
	@ApiOperation(value = "后台修改密码", httpMethod = "POST")
	public AppResult repwd(sys_user_pwd pwdm) throws NoSuchAlgorithmException {
		AppResult app = new AppResult();
		Object object = rUtils.get("login_token:" + super.getToken());
		SysLoginInfo sysLoginInfo = JsonUtils.toObject(object.toString(), SysLoginInfo.class);
		pwdm.setUid(sysLoginInfo.getUid());
		Integer out = loginService.repwd(pwdm);
		app.ok("修改成功");
		if (out == null){
			app.no("修改失败");
		}
		if (out == -1){
			app.no("旧密码错误");
		}
		return app;
	}
}
